class Solution {
public:
    int numberOfWeakCharacters(vector<vector<int>>& properties) {
        sort(properties.begin(), properties.end(), [](const vector<int>& a, const vector<int>& b) {
            if (a[0] == b[0]) {
                return a[1] > b[1];
            }
            return a[0] < b[0];
            });
        stack<int>st;
        int count = 0;
        for (const auto prop : properties) {
            int defense = prop[1];
            while (!st.empty() && st.top() < defense) {
                st.pop();
                count++;
            }
            st.push(defense);
        }
        return count;
    }
};